package com.yanfan.energy.util;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.utils.StringUtils;
import com.yanfan.energy.domain.statist.*;
import com.github.pagehelper.PageInfo;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author caijinkang
 * @date 2022年10月25日 14:28
 */
public class WidgetUtil {

    public static final Integer TYPE_WATER = 1;
    public static final Integer TYPE_ELECTRICITY = 2;
    public static final Integer TYPE_GAS = 3;
    public static final Integer NoBySpikes = 7;
    public static final String HOUR = "hour";
    public static final String WEEK = "week";
    public static final String DAY = "day";
    public static final String MONTH = "month";
    public static final String YEAR = "year";
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final ZoneId zone = ZoneId.systemDefault();

    /**
     * 得到时间范围
     *
     * @param range
     * @param begin
     * @param end
     * @param format
     * @param datetime
     * @return io.github.jhipster.sample.sansheng.tool.statistics.RangeStatistics
     * @author caijinkang
     * @date 2022/10/26 17:17
     */
    public static RangeStatistics findDateByRange(String range, LocalDate begin, LocalDate end, String format, String datetime) {
        List<String> dates = new ArrayList<>();
        // 使用的数据库名称（默认使用全量表）
        String database = "overall_energy_full";
        if (StringUtils.isEmpty(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
            end = end.plusDays(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
                dates.add(formatter.format(date));
            }
        }
        if (DAY.equals(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
            begin = begin.plusDays(-begin.getDayOfMonth() + 1);
            end = end.plusDays(-end.getDayOfMonth() + 1);
            end = end.plusMonths(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
                dates.add(formatter.format(date));
            }
            database = "overall_energy_day";
        } else if (WEEK.equals(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
            begin = begin.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            end = end.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            end = end.plusDays(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
                dates.add(formatter.format(date));
            }
            database = "overall_energy_day";
        } else if (MONTH.equals(range)) {
            begin = begin.plusDays(-begin.getDayOfYear() + 1);
            end = begin.plusMonths(11);
            format = "%Y-%m";
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM").withZone(zone);
            end = end.plusMonths(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusMonths(1)) {
                dates.add(formatter.format(date));
            }
            datetime = "'%Y-%m'";
            database = "overall_energy_month";
        }
        return new RangeStatistics(dates, begin, end, format, datetime);
    }


    public static RangeStatistics getDataPurchaseRange(String range, LocalDate begin, LocalDate end) {
        // key 为日期， value 为 值 （要展示到页面的）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault());

        Map<String, Double> dateMap = new LinkedHashMap();
        if (StringUtils.isEmpty(range)) {
            end = end.plusDays(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
                dateMap.put(formatter.format(date), 0.0);
            }
            new RangeStatistics(dateMap, begin, end, "");
        }
        if (DAY.equals(range)) {
            begin = begin.plusDays(-begin.getDayOfMonth() + 1);
            end = end.plusDays(-end.getDayOfMonth() + 1);
            end = end.plusMonths(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
                dateMap.put(formatter.format(date), 0.0);
            }
        } else if (WEEK.equals(range)) {
            begin = begin.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            end = end.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            end = end.plusDays(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
                dateMap.put(formatter.format(date), 0.0);
            }
        } else if (MONTH.equals(range)) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter
                    .ofPattern("yyyy-MM")
                    .withZone(ZoneId.systemDefault());
            begin = begin.plusDays(-begin.getDayOfYear() + 1);
            end = begin.plusMonths(11);
            end = end.plusMonths(1);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusMonths(1)) {
                dateMap.put(dateTimeFormatter.format(date), 0.0);
            }
        }
        // 时间维度不为 月 查 本月数据
        if (!MONTH.equals(range)) {
            begin = begin.plusDays(-begin.getDayOfMonth() + 1);
            end = end.plusDays(-end.getDayOfMonth() + 1);
            end = end.plusMonths(1);
        }
        return new RangeStatistics(dateMap, begin, end, "");
    }

    /**
     * 填充时间范围
     *
     * @param dates
     * @param statistics
     * @return java.util.List<io.github.jhipster.sample.sansheng.tool.statistics.DateStatistics>
     * @author caijinkang
     * @date 2022/10/26 17:19
     */
    public static List<DateStatistics> findDateStatistics(List<String> dates, List<DateStatistics> statistics) {
        Map<String, Double> map = statistics
                .stream()
                .collect(Collectors.toMap(DateStatistics::getDate, DateStatistics::getNumber));
        return dates
                .stream()
                .map(d -> new DateStatistics(0L, d, map.getOrDefault(d, 0.00)))
                .collect(Collectors.toList());
    }

    /**
     * 计算单价
     *
     * @param dateByStatistics 能耗
     * @param dateStatistics   单价
     * @return java.util.List<io.github.jhipster.sample.sansheng.tool.statistics.DateStatistics>
     * @author caijinkang
     * @date 2022/10/28 10:34
     */
    public static List<DateByStatistics> computeByPrice(List<DateByStatistics> dateByStatistics, List<DateStatistics> dateStatistics) {

        dateByStatistics.forEach(d -> {
            dateStatistics.forEach(p -> {
                if (d.getType() == p.getCount().intValue()) {
                    d.setNumber(new BigDecimal(d.getNumber() * p.getNumber())
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue());
                }
            });
            // 尖点类型为NoBySpikes = 7，尖点单价 = 高峰电价 + (高峰电价 * 0.25)
            if (NoBySpikes.equals(d.getType())) {
                Double price = dateStatistics.stream().filter(p -> p.getCount() == 8).findFirst().get().getNumber();
                d.setNumber(new BigDecimal(d.getNumber() * (price + (price * 0.25)))
                                    .setScale(4, BigDecimal.ROUND_HALF_UP)
                                    .doubleValue());
            }
        });

        return dateByStatistics;
    }

    /**
     * 时间排序
     *
     * @param statistics
     * @param range
     * @return java.util.List<io.github.jhipster.sample.sansheng.tool.statistics.DateStatistics>
     * @author caijinkang
     * @date 2022/10/26 21:19
     */
    public static List<DateStatistics> orderByDate(List<DateStatistics> statistics, String range) {
        statistics.sort((o1, o2) -> {
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
            if (MONTH.equals(range)) {
                format2 = new SimpleDateFormat("yyyy-MM");
            }
            try {
                Date dt1 = format2.parse(o1.getDate());
                Date dt2 = format2.parse(o2.getDate());
                return Long.compare(dt1.getTime(), dt2.getTime());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 0;
        });
        return statistics;
    }

    /**
     * 根据日期和Type去重
     *
     * @param dateByStatistics
     * @return java.util.List<io.github.jhipster.sample.sansheng.tool.statistics.DateByStatistics>
     * @author caijinkang
     * @date 2022/10/26 19:05
     */
    public static List<DateByStatistics> mergeByDateBy(List<DateByStatistics> dateByStatistics) {
        List<DateByStatistics> result = new ArrayList<>();
        if (!dateByStatistics.isEmpty()) {
            dateByStatistics
                    .parallelStream()
                    .collect(Collectors.groupingBy(o -> (o.getDate() + o.getType()), Collectors.toList()))
                    .forEach((id, transfer) -> {
                        transfer
                                .stream()
                                .reduce((a, b) -> new DateByStatistics(a.getType(), a.getDate(), a.getHour(), a.getConsumption(), a.getDosage(), new BigDecimal(a.getNumber() + a.getNumber())
                                        .setScale(6, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue()))
                                .ifPresent(result::add);
                    });
        }
        return result;
    }


    /**
     * 计算时间段的总电价
     *
     * @param dateByStatistics
     * @return java.util.List<io.github.jhipster.sample.sansheng.tool.statistics.DateByStatistics>
     * @author caijinkang
     * @date 2022/10/26 19:05
     */
    public static List<DateStatistics> mergeByElectricityPrice(List<DateByStatistics> dateByStatistics) {
        List<DateStatistics> result1 = new ArrayList<>();
        if (!dateByStatistics.isEmpty()) {
            List<DateStatistics> result = result = dateByStatistics
                    .stream()
                    .map(d -> new DateStatistics(d
                                                         .getType()
                                                         .longValue(), formatter.format(LocalDate.now()), d.getNumber()))
                    .collect(Collectors.toList());

            result
                    .parallelStream()
                    .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                    .forEach((id, transfer) -> {
                        transfer
                                .stream()
                                .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), new BigDecimal(a.getNumber() + b.getNumber())
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue()))
                                .ifPresent(result1::add);
                    });

        }
        return result1;
    }

    /**
     * 计算时间段的总能耗(根据能源类型来总和能耗)
     *
     * @param dateByStatistics
     * @return java.util.List<io.github.jhipster.sample.sansheng.tool.statistics.DateByStatistics>
     * @author caijinkang
     * @date 2022/10/26 19:05
     */
    public static List<DateStatistics> mergeByEnergyPrice(List<DateStatistics> dateByStatistics) {
        List<DateStatistics> result1 = new ArrayList<>();
        if (!dateByStatistics.isEmpty()) {
            dateByStatistics
                    .parallelStream()
                    .collect(Collectors.groupingBy(o -> (o.getCount()), Collectors.toList()))
                    .forEach((id, transfer) -> {
                        transfer
                                .stream()
                                .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), new BigDecimal(a.getNumber() + b.getNumber())
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue()))
                                .ifPresent(result1::add);
                    });

        }
        return result1;
    }


    /**
     * 能源总类型统计(电能(四种加一起))，时间设置为今天，取一种时间一个值
     *
     * @param dateByStatistics
     * @return java.util.List<io.github.jhipster.sample.sansheng.tool.statistics.DateStatistics>
     * @author caijinkang
     * @date 2022/10/31 11:59
     */
    public static List<DateStatistics> mergeElementByNow(List<DateStatistics> dateByStatistics) {
        List<DateStatistics> result = new ArrayList<>();
        if (!dateByStatistics.isEmpty()) {
            dateByStatistics.stream().forEach(d -> {
                {
                    d.setDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                }
            });
            dateByStatistics
                    .stream()
                    .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                    .forEach((id, transfer) -> {
                        transfer
                                .stream()
                                .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), new BigDecimal(a.getNumber() + b.getNumber())
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue()))
                                .ifPresent(result::add);
                    });
        }
        return result;
    }

    /**
     * 能源总类型统计(电能(四种加一起))，时间设置为时间维度的时候
     *
     * @param dateByStatistics
     * @return java.util.List<io.github.jhipster.sample.sansheng.tool.statistics.DateStatistics>
     * @author caijinkang
     * @date 2022/10/31 11:59
     */
    public static List<DateStatistics> mergeElementByOld(List<DateStatistics> dateByStatistics) {
        List<DateStatistics> result = new ArrayList<>();
        if (!dateByStatistics.isEmpty()) {
            dateByStatistics
                    .stream()
                    .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                    .forEach((id, transfer) -> {
                        transfer
                                .stream()
                                .reduce((a, b) -> new DateStatistics(a.getCount(), a.getDate(), new BigDecimal(a.getNumber() + b.getNumber())
                                        .setScale(4, BigDecimal.ROUND_HALF_UP)
                                        .doubleValue()))
                                .ifPresent(result::add);
                    });
        }
        return result;
    }

    /**
     * 设置单位为万元
     *
     * @param dateByStatistics
     * @return java.util.List<com.ruoyi.common.core.domain.model.DateByStatistics>
     * @author caijinkang
     * @date 2022/11/1 11:47
     */
    public static List<DateByStatistics> setPriceByBigUnit(List<DateByStatistics> dateByStatistics) {
        dateByStatistics.stream().forEach(d -> {
            d.setNumber(setPriceByTwoDecimals(d.getNumber() / 10000));
        });
        return dateByStatistics;
    }

    /**
     * 设置保留四位小数
     *
     * @param number
     * @return java.lang.Double
     * @author caijinkang
     * @date 2022/12/1 14:07
     */
    public static Double setPriceByTwoDecimals(Double number) {
        return new BigDecimal(number).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 设置保留两位小数
     *
     * @param number
     * @return java.lang.Double
     * @author caijinkang
     * @date 2022/12/5 9:40
     */
    public static Double setPriceByDecimals(Double number) {
        return new BigDecimal(number).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 根据代入的单价计算
     *
     * @param price
     * @param dateStatistics
     * @return java.util.List<com.ruoyi.common.core.domain.model.DateStatistics>
     * @author caijinkang
     * @date 2022/12/5 9:40
     */
    public static List<DateStatistics> setPriceByDateStatistics(Double price, List<DateStatistics> dateStatistics) {
        if (price > 0 && !dateStatistics.isEmpty()) {
            dateStatistics.stream().forEach(dateStatistics1 -> {
                setPriceByTwoDecimals(dateStatistics1.getNumber() * price / 10000);
            });
        }
        return dateStatistics;
    }

    // todo：新能源

    /**
     * 小时的时间段填充
     *
     * @param range
     * @param begin
     * @param end
     * @param format
     * @param datetime
     * @return com.ruoyi.common.core.domain.model.RangeStatistics
     * @author caijinkang
     * @date 2023/2/20 11:07
     */
    public static RangeStatistics findDatesByRangeBeginAndEnd(String range, LocalDate begin, LocalDate end, String format, String datetime) {
        List<String> dates = new ArrayList<>();
        if (HOUR.equals(range)) {
            for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
                dates.add(formatter.format(date));
            }
        }
        if (DAY.equals(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
            end = end.plusDays(1);
            datetime = "'%H'";
            for (int hour = 0; hour <= 23; hour++) {
                if (hour < 10) {
                    dates.add(String.format("%02d", hour));
                } else {
                    dates.add(hour + "");
                }
            }
        }
        if (MONTH.equals(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(zone);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusDays(1)) {
                dates.add(formatter.format(date));
            }
        }
        if (YEAR.equals(range)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM").withZone(zone);
            for (LocalDate date = begin; date.isBefore(end); date = date.plusMonths(1)) {
                dates.add(formatter.format(date));
            }
        }

        return new RangeStatistics(dates, begin, end, format, datetime);
    }


    public static List<DetailStatistics> findDateStatisticsTry(List<String> dates, List<DetailStatistics> statistics) {
        Map<String, Double> map = statistics
                .stream()
                .collect(Collectors.toMap(DetailStatistics::getDate, DetailStatistics::getCountPrice));
        Map<String, Double> map2 = statistics
                .stream()
                .collect(Collectors.toMap(DetailStatistics::getDate, DetailStatistics::getCountDosage));
        Map<String, Double> map3 = statistics
                .stream()
                .collect(Collectors.toMap(DetailStatistics::getDate, DetailStatistics::getPrice));
        return dates
                .stream()
                .map(d -> new DetailStatistics(map2.getOrDefault(d, 0.00), map.getOrDefault(d, 0.00), d, 0, map3.getOrDefault(d, 0.00)))
                .collect(Collectors.toList());
    }

    /**
     * 优化findDateStatisticsTry方法
     *
     * @param dates
     * @param statistics
     * @return java.util.List<com.ruoyi.common.core.domain.model.DetailStatistics>
     * @author caijinkang
     * @date 2023/2/20 14:40
     */
    public static List<DetailStatistics> findDateStatisticsTry1(List<String> dates, List<DetailStatistics> statistics) {
        Map<String, DetailStatistics> map = statistics
                .stream()
                .collect(Collectors.toMap(DetailStatistics::getDate, Function.identity()));
        return dates
                .stream()
                .map(d -> map.getOrDefault(d, new DetailStatistics(0.00, 0.00, d, 0, 0.00)))
                .collect(Collectors.toList());
    }

    /**
     * 用电数据展示
     *
     * @param electricity
     * @return com.ruoyi.common.core.domain.model.ElectricityStatistics
     * @author caijinkang
     * @date 2022/12/14 14:58
     */
    public static ElectricityStatistics setCountEnergy(ElectricityStatistics electricity) {
        if (electricity != null) {
            electricity.setCountPrice(electricity.getSpikesPrice() + electricity.getPeaksPrice() + electricity.getParityPrice() + electricity.getLowEbbPrice());
            electricity.setCountDosage(electricity.getSpikes() + electricity.getPeaks() + electricity.getParity() + electricity.getLowEbb());
        } else {
            ElectricityStatistics electricityStatistics = new ElectricityStatistics(0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00);
            return electricityStatistics;
        }
        return electricity;
    }

    /**
     * 用水数据展示
     *
     * @return com.ruoyi.common.core.domain.model.WaterStatistics
     * @author caijinkang
     * @date 2022/12/14 15:00
     */
    public static WaterStatistics setCountEnergyByWater(List<DateStatistics> dateStatistics) {
        WaterStatistics waterStatistics = new WaterStatistics();
        if (dateStatistics != null && dateStatistics.size() > 0) {
            // 过滤出三种水，type=1、2、3
            dateStatistics.stream().forEach(d -> {
                if (d.getCount() == 1) {
                    waterStatistics.setCooling(d.getNumber());
                } else if (d.getCount() == 2) {
                    waterStatistics.setSoftened(d.getNumber());
                } else if (d.getCount() == 3) {
                    waterStatistics.setPure(d.getNumber());
                }
            });
        }
        return waterStatistics;
    }

    /**
     * 用气数据展示
     *
     * @return com.ruoyi.common.core.domain.model.WaterStatistics
     * @author caijinkang
     * @date 2022/12/14 15:00
     */
    public static GasStatistics setCountEnergyByGas(List<DateStatistics> dateStatistics) {
        GasStatistics gasStatistics = new GasStatistics();
        if (dateStatistics != null && dateStatistics.size() > 0) {
            // 过滤出三种气，type=4、5、6
            dateStatistics.stream().forEach(d -> {
                if (d.getCount() == 4) {
                    gasStatistics.setCompressed(d.getNumber());
                } else if (d.getCount() == 5) {
                    gasStatistics.setCarbon(d.getNumber());
                } else if (d.getCount() == 6) {
                    gasStatistics.setVapour(d.getNumber());
                }
            });
        }
        return gasStatistics;
    }

    public static WaterStatistics setCountEnergyByWaterPrice(WaterStatistics waterStatistics, Map<Integer, Double> map) {
        Double coolingPrice = map.get(1);
        Double softenedPrice = map.get(2);
        Double purePrice = map.get(3);

        if (waterStatistics.getCooling() == null) {
            waterStatistics.setCooling(0.00);
        }
        if (waterStatistics.getSoftened() == null) {
            waterStatistics.setSoftened(0.00);
        }
        if (waterStatistics.getPure() == null) {
            waterStatistics.setPure(0.00);
        }
        waterStatistics.setCoolingPrice(coolingPrice * waterStatistics.getCooling());
        waterStatistics.setSoftenedPrice(softenedPrice * waterStatistics.getSoftened());
        waterStatistics.setPurePrice(purePrice * waterStatistics.getPure());
        waterStatistics.setCountPrice(waterStatistics.getCoolingPrice() + waterStatistics.getSoftenedPrice() + waterStatistics.getPurePrice());
        waterStatistics.setCountDosage(waterStatistics.getCooling() + waterStatistics.getSoftened() + waterStatistics.getPure());
        return waterStatistics;
    }

    public static GasStatistics setCountEnergyByGasPrice(GasStatistics gasStatistics, Map<Integer, Double> map) {
        Double compressedPrice = map.get(4);
        Double carbonPrice = map.get(5);
        Double vapourPrice = map.get(6);
        if (gasStatistics.getCompressed() == null) {
            gasStatistics.setCompressed(0.00);
        }
        if (gasStatistics.getCarbon() == null) {
            gasStatistics.setCarbon(0.00);
        }
        if (gasStatistics.getVapour() == null) {
            gasStatistics.setVapour(0.00);
        }
        gasStatistics.setCompressedPrice(compressedPrice * gasStatistics.getCompressed());
        gasStatistics.setCarbonPrice(carbonPrice * gasStatistics.getCarbon());
        gasStatistics.setVapourPrice(vapourPrice * gasStatistics.getVapour());
        gasStatistics.setCountPrice(gasStatistics.getCompressedPrice() + gasStatistics.getCarbonPrice() + gasStatistics.getVapourPrice());
        gasStatistics.setCountDosage(gasStatistics.getCompressed() + gasStatistics.getCarbon() + gasStatistics.getVapour());
        return gasStatistics;
    }

    public static List<BuildingStatistics> findBuildingByElectricity(List<BuildingStatistics> buildingStatistics) {
        List<BuildingStatistics> result = new ArrayList<>();
        if (buildingStatistics != null && buildingStatistics.size() > 0) {
            buildingStatistics
                    .stream()
                    .collect(Collectors.groupingBy(o -> (o.getBuildingId()), Collectors.toList()))
                    .forEach((id, transfer) -> {
                        transfer
                                .stream()
                                .reduce((a, b) -> new BuildingStatistics(a.getBuildingId(), a.getBuildingName(), a.getType(), a.getTypeCode(), setPriceByTwoDecimals(a.getSpikesPrice() + a.getPeaksPrice() + a.getParityPrice() + a.getLowEbbPrice()), a.getSpikesPrice(), a.getPeaksPrice(), a.getParityPrice(), a.getLowEbbPrice()))
                                .ifPresent(result::add);
                    });
        }
        return result;
    }

    public static List<WorkshopStatistics> findWorkshopByElectricity(List<WorkshopStatistics> workshopStatistics) {
        List<WorkshopStatistics> result = new ArrayList<>();
        if (workshopStatistics != null && workshopStatistics.size() > 0) {
            workshopStatistics
                    .stream()
                    .collect(Collectors.groupingBy(o -> (o.getWorkshopId()), Collectors.toList()))
                    .forEach((id, transfer) -> {
                        transfer
                                .stream()
                                .reduce((a, b) -> new WorkshopStatistics(a.getWorkshopId(), a.getWorkshopName(), a.getType(), a.getTypeCode(), a.getNumber() + a.getNumber(), a.getElectricity() + a.getElectricity(), null))
                                .ifPresent(result::add);
                    });
        }
        return result;
    }

    public static List<BuildingStatistics> findBuildingByWater(List<BuildingStatistics> buildingStatistics) {
        List<BuildingStatistics> result = new ArrayList<>();
        if (buildingStatistics != null && buildingStatistics.size() > 0) {
            buildingStatistics
                    .stream()
                    .collect(Collectors.groupingBy(o -> (o.getBuildingId()), Collectors.toList()))
                    .forEach((id, transfer) -> {
                        transfer
                                .stream()
                                .reduce((a, b) -> new BuildingStatistics(a.getBuildingId(), a.getBuildingName(), a.getType(), a.getTypeCode(), setPriceByTwoDecimals(a.getNumber() + a.getNumber()), a.getSpikesPrice(), a.getPeaksPrice(), a.getParityPrice(), a.getLowEbbPrice()))
                                .ifPresent(result::add);
                    });
        }
        return result;
    }

    public static List<BuildingStatistics> findBuildingByWaterGasPrice(List<BuildingStatistics> buildingStatistics, Map<Integer, Double> map) {
        Double coolingPrice = map.get(1);
        Double softenedPrice = map.get(2);
        Double purePrice = map.get(3);
        Double compressedPrice = map.get(4);
        Double carbonPrice = map.get(5);
        Double vapourPrice = map.get(6);

        buildingStatistics.stream().forEach(build -> {
            if (build.getTypeCode() == 1) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * coolingPrice));
            }
            if (build.getTypeCode() == 2) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * softenedPrice));
            }
            if (build.getTypeCode() == 3) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * purePrice));
            }
            if (build.getTypeCode() == 4) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * compressedPrice));
            }
            if (build.getTypeCode() == 5) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * carbonPrice));
            }
            if (build.getTypeCode() == 6) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * vapourPrice));
            }
        });

        return buildingStatistics;
    }

    public static List<WorkshopStatistics> findWorkshopByWaterGasPrice(List<WorkshopStatistics> workshopStatistics, Map<Integer, Double> map) {
        Double coolingPrice = map.get(1);
        Double softenedPrice = map.get(2);
        Double purePrice = map.get(3);
        Double compressedPrice = map.get(4);
        Double carbonPrice = map.get(5);
        Double vapourPrice = map.get(6);

        workshopStatistics.stream().forEach(build -> {
            if (build.getTypeCode() == 1) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * coolingPrice));
            }
            if (build.getTypeCode() == 2) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * softenedPrice));
            }
            if (build.getTypeCode() == 3) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * purePrice));
            }
            if (build.getTypeCode() == 4) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * compressedPrice));
            }
            if (build.getTypeCode() == 5) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * carbonPrice));
            }
            if (build.getTypeCode() == 6) {
                build.setNumber(setPriceByTwoDecimals(build.getNumber() * vapourPrice));
            }
        });

        return workshopStatistics;
    }

    public static AjaxResult setBuildingProportion(UtilitiesStatistics utilities, List<BuildingStatistics> buildingStatistics, Map<Integer, Double> setting) {
        // 查询水
        if (TYPE_WATER.equals(utilities.getType())) {
            findBuildingByWaterGasPrice(buildingStatistics, setting);
            return AjaxResult.success(findBuildingByWater(findBuildingByWaterGasPrice(buildingStatistics, setting)
                                                                  .stream()
                                                                  .filter(b -> b.getTypeCode() == 1 || b.getTypeCode() == 2 || b.getTypeCode() == 3)
                                                                  .collect(Collectors.toList())));
        }
        // 查询电
        if (TYPE_ELECTRICITY.equals(utilities.getType())) {
            // 取number的值为电的累加
            return AjaxResult.success(findBuildingByElectricity(buildingStatistics));
        }
        // 查询气
        if (TYPE_GAS.equals(utilities.getType())) {
            findBuildingByWaterGasPrice(buildingStatistics, setting);
            return AjaxResult.success(findBuildingByWater(findBuildingByWaterGasPrice(buildingStatistics, setting)
                                                                  .stream()
                                                                  .filter(b -> b.getTypeCode() == 4 || b.getTypeCode() == 5 || b.getTypeCode() == 6)
                                                                  .collect(Collectors.toList())));
        }
        return AjaxResult.error("查询失败");
    }

    public static AjaxResult setWorkshopProportion(UtilitiesStatistics utilities, List<WorkshopStatistics> workshopStatistics, Map<Integer, Double> setting) {
        workshopStatistics.stream().forEach(workshopStatistics1 -> {
            if (Objects.isNull(workshopStatistics1.getWorkshopId())) {
                workshopStatistics1.setWorkshopId(1);
            }
        });
        List<WorkshopStatistics> workshopStatisticsList = findWorkshopByElectricity(workshopStatistics);
        workshopStatisticsList.stream().forEach(w -> {
            w.setNumber(setPriceByTwoDecimals(w.getNumber() + w.getElectricity()));
            w.setElectricity(setPriceByTwoDecimals(0.00));
            w.setCountDosage(NumberUtils.amountConversion(w.getNumber() + w.getElectricity()));
        });
        if (!Objects.isNull(workshopStatisticsList)) {
            return AjaxResult.success(workshopStatisticsList);
        }
        return AjaxResult.error("查询失败");
    }

    public static AjaxResult setUtilities(UtilitiesStatistics utilities, List<DateStatistics> waterAndGas, ElectricityStatistics electricity, Map<Integer, Double> setting) {
        // 查询水
        if (TYPE_WATER.equals(utilities.getType())) {
            // 过滤出三种水
            WaterStatistics waterStatistics = setCountEnergyByWater(waterAndGas);
            return AjaxResult.success(setWaterUnitStatistics(setCountEnergyByWaterPrice(waterStatistics, setting)));
        }
        // 查询电
        if (TYPE_ELECTRICITY.equals(utilities.getType())) {
            return AjaxResult.success(setUnitElectricityStatistics(setCountEnergy(electricity)));
        }
        // 查询气
        if (TYPE_GAS.equals(utilities.getType())) {
            GasStatistics gasStatistics = setCountEnergyByGas(waterAndGas);
            return AjaxResult.success(setGasUnitStatistics(setCountEnergyByGasPrice(gasStatistics, setting)));
        }
        return AjaxResult.error("查询失败");
    }

    public static AjaxResult setElectricProportion(UtilitiesStatistics utilities, List<DateStatistics> waterAndGas, ElectricityStatistics electricity, Map<Integer, Double> setting) {
        // 查询水
        if (TYPE_WATER.equals(utilities.getType())) {
            WaterStatistics waterStatistics = setCountEnergyByWater(waterAndGas);
            // 计算单价
            WaterStatistics water = setCountEnergyByWaterPrice(waterStatistics, setting);
            // 封装新对象
            WaterBody waterBody = new WaterBody();
            waterBody.setCooling(setPriceByTwoDecimals(water.getCoolingPrice()));
            waterBody.setSoftened(setPriceByTwoDecimals(water.getSoftenedPrice()));
            waterBody.setPure(setPriceByTwoDecimals(water.getPurePrice()));
            return AjaxResult.success(waterBody);
        }
        // 查询电
        if (TYPE_ELECTRICITY.equals(utilities.getType())) {
            ElectricityStatistics electricityStatistics = setCountEnergy(electricity);
            // 封装新对象
            ElectricityBody electricityBody = new ElectricityBody();
            electricityBody.setSpikes(setPriceByTwoDecimals(electricityStatistics.getSpikesPrice()));
            electricityBody.setPeaks(setPriceByTwoDecimals(electricityStatistics.getPeaksPrice()));
            electricityBody.setParity(setPriceByTwoDecimals(electricityStatistics.getParityPrice()));
            electricityBody.setLowEbb(setPriceByTwoDecimals(electricityStatistics.getLowEbbPrice()));

            return AjaxResult.success(electricityBody);
        }
        // 查询气
        if (TYPE_GAS.equals(utilities.getType())) {
            GasStatistics gasStatistics = setCountEnergyByGas(waterAndGas);
            GasStatistics gas = setCountEnergyByGasPrice(gasStatistics, setting);
            // 封装新对象
            GasBody gasBody = new GasBody();
            gasBody.setCompressed(setPriceByTwoDecimals(gas.getCompressedPrice()));
            gasBody.setCarbon(setPriceByTwoDecimals(gas.getCarbonPrice()));
            gasBody.setVapour(setPriceByTwoDecimals(gas.getVapourPrice()));
            return AjaxResult.success(gasBody);
        }
        return AjaxResult.error("查询失败");
    }


    public static List<DetailStatistics> setEnergyByDetailPrice(List<DetailStatistics> detailStatistics) {
        detailStatistics.stream().forEach(d -> {
            if (d.getCountDosage() != 0) {
                d.setPrice(setPriceByDecimals(d.getCountPrice() / d.getCountDosage()));
            }
        });
        return detailStatistics;
    }

    public static List<DetailStatistics> setEnergyByWaterGayDetailPrice(List<DetailStatistics> detailStatistics, Map<Integer, Double> map) {
        Double coolingPrice = map.get(1);
        Double softenedPrice = map.get(2);
        Double purePrice = map.get(3);
        Double compressedPrice = map.get(4);
        Double carbonPrice = map.get(5);
        Double vapourPrice = map.get(6);

        detailStatistics.stream().forEach(d -> {
            if (d.getType() == 1 || "1".equals(d.getType())) {
                d.setCountPrice(setPriceByTwoDecimals(d.getCountDosage() * coolingPrice));
            }
            if (d.getType() == 2 || "2".equals(d.getType())) {
                d.setCountPrice(setPriceByTwoDecimals(d.getCountDosage() * softenedPrice));
            }
            if (d.getType() == 3 || "3".equals(d.getType())) {
                d.setCountPrice(setPriceByTwoDecimals(d.getCountDosage() * purePrice));
            }
            if (d.getType() == 4 || "4".equals(d.getType())) {
                d.setCountPrice(setPriceByTwoDecimals(d.getCountDosage() * compressedPrice));
            }
            if (d.getType() == 5 || "5".equals(d.getType())) {
                d.setCountPrice(setPriceByTwoDecimals(d.getCountDosage() * carbonPrice));
            }
            if (d.getType() == 6 || "6".equals(d.getType())) {
                d.setCountPrice(setPriceByTwoDecimals(d.getCountDosage() * vapourPrice));
            }
        });

        return detailStatistics;
    }

    public static List<DetailStatistics> findEnergyByWaterGayDetailPrice(List<DetailStatistics> buildingStatistics) {
        List<DetailStatistics> result = new ArrayList<>();
        if (buildingStatistics != null && buildingStatistics.size() > 0) {
            buildingStatistics
                    .stream()
                    .collect(Collectors.groupingBy(o -> (o.getDate()), Collectors.toList()))
                    .forEach((id, transfer) -> {
                        transfer
                                .stream()
                                .reduce((a, b) -> new DetailStatistics(a.getCountDosage() + a.getCountDosage(), a.getCountPrice() + a.getCountPrice(), a.getDate(), a.getType(), a.getPrice()))
                                .ifPresent(result::add);
                    });
        }
        return setEnergyByWaterGayDetailPrice(result);
    }

    public static List<DetailStatistics> setEnergyByWaterGayDetailPrice(List<DetailStatistics> buildingStatistics) {
        if (buildingStatistics != null && buildingStatistics.size() > 0) {
            buildingStatistics.stream().forEach(b -> {
                if (b.getCountDosage() != 0 && b.getCountDosage() != null && b.getCountPrice() != null) {
                    b.setPrice(setPriceByTwoDecimals(b.getCountPrice() / b.getCountDosage()));
                } else {
                    b.setPrice(0.00);
                }
            });
        }
        return buildingStatistics;
    }

    public static AjaxResult setFindElectricDetails(UtilitiesStatistics utilities, RangeStatistics rangeStatistics, List<DetailStatistics> electricity, List<DetailStatistics> waterAndGay, Map<Integer, Double> setting) {
        // 查询水
        if (TYPE_WATER.equals(utilities.getType())) {
            List<DetailStatistics> detailStatistics = findDateStatisticsTry(rangeStatistics.getDates(), findEnergyByWaterGayDetailPrice(setEnergyByWaterGayDetailPrice(waterAndGay, setting)
                                                                                                                                                .stream()
                                                                                                                                                .filter(w -> w.getType() == 1 || w.getType() == 2 || w.getType() == 3)
                                                                                                                                                .collect(Collectors.toList())));
            PageInfo pageInfo = new PageInfo<>(setUnitDetailStatistics(detailStatistics)
                                                       .stream()
                                                       .skip((utilities.getCurrPage() - 1) * utilities.getPageSize())
                                                       .limit(utilities.getPageSize())
                                                       .collect(Collectors.toList()));
            pageInfo.setTotal(detailStatistics.size());
            return AjaxResult.success(pageInfo);
        }
        // 查询电
        if (TYPE_ELECTRICITY.equals(utilities.getType())) {
            List<DetailStatistics> detailStatistics = findDateStatisticsTry(rangeStatistics.getDates(), setEnergyByDetailPrice(electricity));
            PageInfo pageInfo = new PageInfo<>(setUnitDetailStatistics(detailStatistics)
                                                       .stream()
                                                       .skip((utilities.getCurrPage() - 1) * utilities.getPageSize())
                                                       .limit(utilities.getPageSize())
                                                       .collect(Collectors.toList()));
            pageInfo.setTotal(detailStatistics.size());
            return AjaxResult.success(pageInfo);
        }
        // 查询气
        if (TYPE_GAS.equals(utilities.getType())) {
            List<DetailStatistics> detailStatistics = findDateStatisticsTry(rangeStatistics.getDates(), findEnergyByWaterGayDetailPrice(setEnergyByWaterGayDetailPrice(waterAndGay, setting)
                                                                                                                                                .stream()
                                                                                                                                                .filter(w -> w.getType() == 4 || w.getType() == 5 || w.getType() == 6)
                                                                                                                                                .collect(Collectors.toList())));
            PageInfo pageInfo = new PageInfo<>(setUnitDetailStatistics(detailStatistics)
                                                       .stream()
                                                       .skip((utilities.getCurrPage() - 1) * utilities.getPageSize())
                                                       .limit(utilities.getPageSize())
                                                       .collect(Collectors.toList()));
            pageInfo.setTotal(detailStatistics.size());
            return AjaxResult.success(pageInfo);
        }
        return AjaxResult.error("查询失败");
    }

    public static WaterUnitStatistics setWaterUnitStatistics(WaterStatistics waterStatistics) {
        WaterUnitStatistics waterUnitStatistics = new WaterUnitStatistics();
        waterUnitStatistics.setCountPrice(NumberUtils.amountConversion(waterStatistics.getCountPrice()));
        waterUnitStatistics.setCountDosage(NumberUtils.amountConversion(waterStatistics.getCountDosage()));
        waterUnitStatistics.setCoolingPrice(NumberUtils.amountConversion(waterStatistics.getCoolingPrice()));
        waterUnitStatistics.setSoftenedPrice(NumberUtils.amountConversion(waterStatistics.getSoftenedPrice()));
        waterUnitStatistics.setPurePrice(NumberUtils.amountConversion(waterStatistics.getPurePrice()));
        waterUnitStatistics.setCooling(NumberUtils.amountConversion(waterStatistics.getCooling()));
        waterUnitStatistics.setSoftened(NumberUtils.amountConversion(waterStatistics.getSoftened()));
        waterUnitStatistics.setPure(NumberUtils.amountConversion(waterStatistics.getPure()));
        return waterUnitStatistics;
    }

    public static ElectricityUnitStatistics setUnitElectricityStatistics(ElectricityStatistics electricityStatistics) {
        ElectricityUnitStatistics electricityUnitStatistics = new ElectricityUnitStatistics();
        electricityUnitStatistics.setCountPrice(NumberUtils.amountConversion(electricityStatistics.getCountPrice()));
        electricityUnitStatistics.setCountDosage(NumberUtils.amountConversion(electricityStatistics.getCountDosage()));
        electricityUnitStatistics.setSpikesPrice(NumberUtils.amountConversion(electricityStatistics.getSpikesPrice()));
        electricityUnitStatistics.setPeaksPrice(NumberUtils.amountConversion(electricityStatistics.getPeaksPrice()));
        electricityUnitStatistics.setParityPrice(NumberUtils.amountConversion(electricityStatistics.getParityPrice()));
        electricityUnitStatistics.setLowEbbPrice(NumberUtils.amountConversion(electricityStatistics.getLowEbbPrice()));
        electricityUnitStatistics.setSpikes(NumberUtils.amountConversion(electricityStatistics.getSpikes()));
        electricityUnitStatistics.setPeaks(NumberUtils.amountConversion(electricityStatistics.getPeaks()));
        electricityUnitStatistics.setParity(NumberUtils.amountConversion(electricityStatistics.getParity()));
        electricityUnitStatistics.setLowEbb(NumberUtils.amountConversion(electricityStatistics.getLowEbb()));
        return electricityUnitStatistics;
    }

    public static GasUnitStatistics setGasUnitStatistics(GasStatistics gasStatistics) {
        GasUnitStatistics gasUnitStatistics = new GasUnitStatistics();
        gasUnitStatistics.setCountPrice(NumberUtils.amountConversion(gasStatistics.getCountPrice()));
        gasUnitStatistics.setCountDosage(NumberUtils.amountConversion(gasStatistics.getCountDosage()));
        gasUnitStatistics.setCompressedPrice(NumberUtils.amountConversion(gasStatistics.getCompressedPrice()));
        gasUnitStatistics.setVapourPrice(NumberUtils.amountConversion(gasStatistics.getVapourPrice()));
        gasUnitStatistics.setCarbonPrice(NumberUtils.amountConversion(gasStatistics.getCarbonPrice()));
        gasUnitStatistics.setCompressed(NumberUtils.amountConversion(gasStatistics.getCompressed()));
        gasUnitStatistics.setVapour(NumberUtils.amountConversion(gasStatistics.getVapour()));
        gasUnitStatistics.setCarbon(NumberUtils.amountConversion(gasStatistics.getCarbon()));
        return gasUnitStatistics;
    }

    public static List<DetailUnitStatistics> setUnitDetailStatistics(List<DetailStatistics> detailStatisticsList) {
        List<DetailUnitStatistics> detailUnitStatisticsList = detailStatisticsList
                .stream()
                .map(d -> new DetailUnitStatistics(NumberUtils.amountConversion(d.getCountDosage()), NumberUtils.amountConversion(d.getCountPrice()), d.getDate(), d.getType(), NumberUtils.amountConversion(d.getPrice())))
                .collect(Collectors.toList());
        return detailUnitStatisticsList;
    }

    public static List<BuildingUnitStatistics> setUnitBuildingStatistics(List<BuildingStatistics> buildingStatisticsList) {
        List<BuildingUnitStatistics> buildingUnitStatistics = buildingStatisticsList
                .stream()
                .map(b -> new BuildingUnitStatistics(b.getBuildingId(), b.getBuildingName(), b.getType(), b.getTypeCode(), NumberUtils.amountConversion(b.getNumber()), NumberUtils.amountConversion(b.getSpikesPrice()), NumberUtils.amountConversion(b.getPeaksPrice()), NumberUtils.amountConversion(b.getParityPrice()), NumberUtils.amountConversion(b.getLowEbbPrice())))
                .collect(Collectors.toList());
        return buildingUnitStatistics;
    }

    public static AjaxResult setFindWorkshopElectricDetails(UtilitiesStatistics utilities, RangeStatistics rangeStatistics, List<DetailStatistics> electricity, List<DetailStatistics> waterAndGay, Map<Integer, Double> setting) {

        List<DetailStatistics> water = findEnergyByWaterGayDetailPrice(setEnergyByWaterGayDetailPrice(waterAndGay, setting)
                                                                               .stream()
                                                                               .filter(w -> w.getType() == 1 || w.getType() == 2 || w.getType() == 3)
                                                                               .collect(Collectors.toList()));
        List<DetailStatistics> gas = findEnergyByWaterGayDetailPrice(setEnergyByWaterGayDetailPrice(waterAndGay, setting)
                                                                             .stream()
                                                                             .filter(w -> w.getType() == 4 || w.getType() == 5 || w.getType() == 6)
                                                                             .collect(Collectors.toList()));

        water.addAll(gas);
        electricity.stream().forEach(e -> {
            e.setType(7);
        });

        water.addAll(electricity);

        if (!Objects.isNull(setFindWorkshopStatistics(rangeStatistics.getDates(), water))) {
            List<EnergyRecordUnitStatistics> energyRecordUnitStatistics = setFindWorkshopStatistics(rangeStatistics.getDates(), water)
                    .stream()
                    .map(d -> new EnergyRecordUnitStatistics(d.getDate(), NumberUtils.amountConversion(d.getWaterDosage()), NumberUtils.amountConversion(d.getElectricityDosage()), NumberUtils.amountConversion(d.getGasDosage())))
                    .collect(Collectors.toList());
            return AjaxResult.success(energyRecordUnitStatistics);
        }
        ;

        return AjaxResult.error("查询失败");
    }

    public static List<EnergyRecordStatistics> setFindWorkshopStatistics(List<String> dates, List<DetailStatistics> statistics) {
        Map<String, Double> map = statistics
                .stream()
                .filter(s -> s.getType().equals(1))
                .collect(Collectors.toMap(DetailStatistics::getDate, DetailStatistics::getCountDosage));
        Map<String, Double> map2 = statistics
                .stream()
                .filter(s -> s.getType().equals(4))
                .collect(Collectors.toMap(DetailStatistics::getDate, DetailStatistics::getCountDosage));
        Map<String, Double> map3 = statistics
                .stream()
                .filter(s -> s.getType().equals(7))
                .collect(Collectors.toMap(DetailStatistics::getDate, DetailStatistics::getCountDosage));

        return dates
                .stream()
                .map(d -> new EnergyRecordStatistics(d, map.getOrDefault(d, 0.00), map3.getOrDefault(d, 0.00), map2.getOrDefault(d, 0.00)))
                .collect(Collectors.toList());
    }

    public static List<DateStatistics> mergePOByDate(List<DateStatistics> PO, List<DateStatistics> value) {
        List<DateStatistics> result = new ArrayList<>();
        List<DateStatistics> dateStatistics = new ArrayList<>();
        dateStatistics.addAll(value);
        dateStatistics.addAll(PO);

        if (!dateStatistics.isEmpty()) {
            // 根据DATE属性相同累加并且合并成一条
            Map<String, Double> map = dateStatistics
                    .stream()
                    .collect(Collectors.groupingBy(DateStatistics::getDate, Collectors.summingDouble(DateStatistics::getNumber)));
            result = map
                    .entrySet()
                    .stream()
                    .map(entry -> new DateStatistics(0L, entry.getKey(), entry.getValue()))
                    .collect(Collectors.toList());
        }
        // 判断是否为负数 负数->0
        result.stream().forEach(r -> {
            r.setNumber(mergeNumber(r.getNumber()));
        });
        return result;
    }

    public static Double mergeNumber(Double number) {
        return judgeMinus(number) ? 0.00 : number;
    }

    public static boolean judgeMinus(Double number) {
        // 判断Double类型的小数是负数还是正数，如果是负数返回true
        if (number < 0.00) {
            return true;
        } else {
            return false;
        }
    }

    public static Date nextTimingDate(Integer type, Date time) {
        if (type == 1) {
            LocalDate date = time.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDateTime dateTime = date.atStartOfDay();
            LocalDateTime nextDaySameHour = dateTime.plus(Duration.ofDays(1)).withHour(dateTime.getHour());
            return Date.from(nextDaySameHour.atZone(ZoneId.systemDefault()).toInstant());
        }
        if (type == 2) {
            LocalDate date = time.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDateTime dateTime = date.atStartOfDay();
            LocalDateTime nextDaySameHour = dateTime.plusWeeks(1);
            return Date.from(nextDaySameHour.atZone(ZoneId.systemDefault()).toInstant());
        }
        if (type == 3) {
            LocalDate date = time.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDateTime dateTime = date.atStartOfDay();
            LocalDateTime nextDaySameHour = dateTime.plusMonths(1);
            return Date.from(nextDaySameHour.atZone(ZoneId.systemDefault()).toInstant());
        }
        return new Date();
    }

    public static Double getProperty(List<DateStatistics> dateStatistics, Long type, Double K) {
        Optional<DateStatistics> optional = dateStatistics
                .stream()
                .filter(human -> human.getCount().equals(type))
                .findFirst();
        Double number = 0.00;
        if (optional.isPresent()) {
            number = optional.get().getNumber();
        } else {
            number = 0.00;
        }
        number = number - K;
        System.out.println("type的值：" + number);
        if (K != 0) {
            return setPriceByDecimals(K / number);
        } else {
            return 0.00;
        }
    }


    public static void main(String[] args) {
        Map map = new LinkedHashMap();
        map.put("2022-10-10", new DateStatistics(1L, "2022-10-10", 0.00));
        System.out.println(map);
    }


}
